<?php
class TampilkanFKC extends penjualan_model{
function Tampilkan_table_diff(){
		$temp="";
		$query0 = $this->db->query("select * from cabang_persen");
		$temp.="<table width='80%' border=1 align='center' padding='1'>";
		$temp.="<th>baby cabang</th><th>hitungan persen</th>";
		foreach($query0->result() as $row){
			$query = $this->get_finds_empty_cabang($row->intid_cabang);
			if($query->num_rows() > 0){
				$temp.="<tr>";
				$query1 = $this->db->query("select * from cabang,cabang_persen where cabang.intid_cabang = ".$row->intid_cabang." and cabang.intid_cabang = cabang_persen.intid_cabang");
				foreach($query1->result() as $rok){
					$temp.="<td align='center'>".$rok->strnama_cabang."</td><td align='center'>".$rok->nilai_persen."</td>";				
					}
				$temp.="</tr>";
			}
		}
		$temp.="</table>";
		return $temp;
	}
	function pengecekan_baby_nominal_differents($id){
		$query = $this->db->query("select * from cabang_persen where intid_cabang = $id");
		return $query;
	}
	function get_baby_fields($id){
		$query = $this->db->query("select baby_cabang.intid_cabang from cabang,baby_cabang where baby_cabang.intid_cabang2 = cabang.intid_cabang and baby_cabang.intid_cabang2 = $id");
		return $query->result();
	}
	function get_finds_empty_cabang($id){
		$query = $this->db->query("select * from cabang,baby_cabang where cabang.intid_cabang = $id");
		return $query;
	}
	function get_baby_FKC($data){
			$this->load->helper("text");
			$this->load->helper("html");
			$this->load->model("penjualan_model");
				$regD = 0;
				$hutD = 0;
				$har_hut = 0;
				$har_reg = 0;
				$reg_total = 0;
				$hut_total = 0;
				$data_cabang = "";
				$temp[] = 0;				
				$omsetreg20=0;
				$omsetreg10=0;
				$omsetdealerreg=0;
				$result = "";
				$id = "";
				$id = $data['id_cabang'];
			//echo "id : ".$id." select b.intid_cabang id_cabang from cabang c,baby_cabang b where c.intid_cabang = b.intid_cabang2 AND c.intid_cabang = ".$id;
				//$data_cabang = $this->Penjualan_model->get_cabang($id);
				//echo "kepala cabang : ".$data_cabang->strkepala_cabang.", ";
				
			$db = $this->db->query("select b.intid_cabang id_cabang from cabang c,baby_cabang b where c.intid_cabang = b.intid_cabang2 AND c.intid_cabang = $id");	
			foreach($db->result() as $rock){
				$result .= $rock->id_cabang." ,";
					//reguler
					$Reg = $this->Penjualan_model->get_reguler($rock->id_cabang, $data['bulan']);
					$RegDealer = $this->Penjualan_model->get_reguler_dealer($rock->id_cabang, $data['bulan']);
			
		
					//Hut
					$Hut = $this->Penjualan_model->get_hut($rock->id_cabang, $data['bulan']);
					$HutDealer = $this->Penjualan_model->get_hut_dealer($rock->id_cabang, $data['bulan']);
					
					//Challenge
					$Cal = $this->Penjualan_model->get_cal($rock->id_cabang, $data['bulan']);
					$CalDealer = $this->Penjualan_model->get_cal_dealer($rock->id_cabang, $data['bulan']);
					
					//Trade In
					$tin = $this->Penjualan_model->get_tin($rock->id_cabang, $data['bulan']);
					$tinDealer = $this->Penjualan_model->get_tin_dealer($rock->id_cabang, $data['bulan']);
					
					//1 Free 1 Hut
					$ifi = $this->Penjualan_model->get_ifi($rock->id_cabang, $data['bulan']);
					$ifiDealer = $this->Penjualan_model->get_ifi_dealer($rock->id_cabang, $data['bulan']);
					
					//1 Free 1 
					$ifo = $this->Penjualan_model->get_ifo($rock->id_cabang, $data['bulan']);
					$ifoDealer = $this->Penjualan_model->get_ifo_dealer($rock->id_cabang, $data['bulan']);
		
					//Netto 
					$net = $this->Penjualan_model->get_net($rock->id_cabang, $data['bulan']);
					$netDealer = $this->Penjualan_model->get_net_dealer($rock->id_cabang, $data['bulan']);
					
					//SpecialPrice 
					$sp = $this->Penjualan_model->get_sp($rock->id_cabang, $data['bulan']);
					$spDealer = $this->Penjualan_model->get_sp_dealer($rock->id_cabang, $data['bulan']);
					
					//Point 
					$p = $this->Penjualan_model->get_p($rock->id_cabang, $data['bulan']);
					$pDealer = $this->Penjualan_model->get_p_dealer($rock->id_cabang, $data['bulan']);
		
					$getFeeSc = $this->Penjualan_model->get_dealer_cabang($rock->id_cabang, $data['bulan']);
					$data_cabang = $this->Penjualan_model->get_cabang($rock->id_cabang);		
					echo " kepala cabang : ".$data_cabang->strkepala_cabang.", ";
				foreach ($Reg as $row):
					     $omsetreg20= $omsetreg20 + $row->TGroup20;
						 $omsetreg10= $omsetreg10 + $row->TGroup10;
				endforeach;	
				foreach ($RegDealer as $row1):
					     $omsetdealerreg= $omsetdealerreg + $row1->RegDealer;
				endforeach;	 
				if(empty($omsetreg20)){
					$omsetreg20 = '0';
					}else{
						//$omsetreg20 = number_format($omsetreg20);
						$omsetreg20 = $omsetreg20;
				}
				if(empty($omsetreg10)){
					$omsetreg10 = '0';
					}else{
						//$omsetreg10 = number_format($omsetreg10);
						$omsetreg10 = $omsetreg10;
				}
				$hdealer = ($omsetreg20 - ($omsetreg20*0.2)) + ($omsetreg10 - ($omsetreg10*0.1));
                if(empty($hdealer)){
						$hdealer = '0';
					}else{
						$hdealer = number_format($hdealer);
				}
				if(!empty($hdealer)){
						$regD = $hdealer;
						$har_reg =  (($hdealer) * 1/100) - (($hdealer* 1/100) * 5/100);
						if(empty($har_reg)){
							$har_reg = '0';
						}else{
							//$har_reg = number_format($har_reg);
							$har_reg = $har_reg;
						}
					}
			
			}
			/*
			if(!empty($data_cabang->strnama_cabang)){
				$temp[1] = $data_cabang->strnama_cabang;
			}else{
				$temp[1] = "";
			}
			if(empty($data_cabang->strkepala_cabang)){
					$temp[0] = $data_cabang = "";
			}else{
					$temp[0] = $data_cabang->strkepala_cabang;
			}	
				$result .= '
                <tr>
                	<td>'.$temp[0].'</td>
                    <td>PIMPINAN</td>
                    <td>'. $temp[1].'</td>
                    <td>Reguler</td>
					
                    <td align="center">'. $omsetreg20.'</td>
                    <td align="center">'. $omsetreg10.'</td>
                    <td align="center">0</td>
                    <td align="center">'. $hdealer.'</td>
                    <td align="center">'. empty($hdealer)?'0':number_format(($hdealer) * 1/100).'</td>
                    <td align="center">'. empty($hdealer)?'0':number_format(($hdealer * 1/100) * 5/100).'</td>
                    <td align="center">'. $har_reg.'</td>
                </tr>';
			}
				/*
				$omsethut20=0;
				$omsethut10=0;
				$omsetdealerhut=0;
				foreach ($Hut as $row2):
					     $omsethut20= $omsethut20 + $row2->TGroup20;
						 $omsethut10= $omsethut10 + $row2->TGroup10;
				endforeach;	
				foreach ($HutDealer as $row3):
					     $omsetdealerhut= $omsetdealerhut + $row3->HutDealer;
				endforeach;	 
				?>
                <tr>
                	<td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>Chall Hut</td>
                    <td align="center"><?php echo (empty($omsethut20))?'0':number_format($omsethut20)?></td>
                    <td align="center"><?php echo (empty($omsethut10))?'0':number_format($omsethut10)?></td>
                    <td align="center">0</td>
                    <td align="center"><?php $hdealerhut = ($omsethut20 - ($omsethut20*0.2)) + ($omsethut10 - ($omsethut10*0.1));
					echo (empty($hdealerhut))?'0':number_format($hdealerhut)?></td>
                    <td align="center"><?php echo (empty($hdealerhut))?'0':number_format(($hdealerhut) * 1/100); $fhut=($hdealerhut * 1)/100?></td>
                    <td align="center"><?php echo (empty($hdealerhut))?'0':number_format(($fhut*5)/100);?></td>
                    <td align="center"><?php 
					
					if(!empty($hdealerhut)){
						$hutD = $hdealerhut;
						$har_hut = (($hdealerhut) * 1/100) - (($hdealerhut* 1/100) * 5/100);
						echo (empty($har_hut))?'0':number_format($har_hut);
					}
					?></td>
                </tr>
                <?php 
				$omsetcal20=0;
				$omsetcal10=0;
				$omsetdealercal=0;
				foreach ($Cal as $row4):
					     $omsettulipcal20= $omsettulipcal20 + $row4->TGroup20;
						 $omsettulipcal10= $omsettulipcal10 + $row4->TGroup10;
				endforeach;	
				foreach ($CalDealer as $row5):
					     $omsetdealercal= $omsetdealercal + $row5->CalDealer;
				endforeach;	 
				?>
                <tr>
                	<td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>Challenge</td>
                    <td align="center"><?php echo (empty($omsetcal20))?'0':number_format($omsetcal20)?></td>
                    <td align="center"><?php echo (empty($omsetcal10))?'0':number_format($omsetcal10)?></td>
                    <td align="center">0</td>
                    <td align="center"><?php $hdealercal = ($omsetcal20 - ($omsetcal20*0.2)) + ($omsetcal10 - ($omsetcal10*0.1));
					echo (empty($hdealercal))?'0':number_format($hdealercal)?></td>
                    <td align="center"><?php echo (empty($hdealercal))?'0':number_format(($hdealercal) * 1/100)?></td>
                    <td align="center"><?php echo (empty($hdealercal))?'0':number_format(($hdealercal * 1/100)*5/100)?></td>
                    <td align="center"><?php 
					$calD=0;
					$har_cal=0;
					if(!empty($hdealercal)){
						$calD = $hdealercal;
						$har_cal = (($hdealercal) * 1/100) - (($hdealercal* 1/100) * 5/100);
						echo (empty($har_cal))?'0':number_format($har_cal);
					}
					?></td>
                </tr>
                <?php 
				$omsettin20=0;
				$omsettin10=0;
				$omsetdealertin=0;
				foreach ($tin as $row6):
					     $omsettin20= $omsettin20 + $row6->TGroup20;
						 $omsettin10= $omsettin10 + $row6->TGroup10;
				endforeach;	
				foreach ($tinDealer as $row7):
					     $omsetdealertin= $omsetdealertin + $row7->tinDealer;
				endforeach;	 
				?>
                <tr>
                	<td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>Trade In</td>
                    <td align="center"><?php echo (empty($omsettin20))?'0':number_format($omsettin20)?></td>
                    <td align="center"><?php echo (empty($omsettin10))?'0':number_format($omsettin10)?></td>
                    <td align="center">0</td>
                    <td align="center"><?php $hdealertin = ($omsettin20 - ($omsettin20*0.2)) + ($omsettin10 - ($omsettin10*0.1));
					echo (empty($hdealertin))?'0':number_format($hdealertin)?></td>
                    <td align="center"><?php echo (empty($hdealertin))?'0':number_format(($hdealertin) * 1/100)?></td>
                    <td align="center"><?php echo (empty($hdealertin))?'0':number_format(($hdealertin * 1/100)*5/100)?></td>
                    <td align="center"><?php 
					$tinD=0;
					$har_tin=0;
					if(!empty($hdealertin)){
						$tinD = $hdealertin;
						$har_tin = (($hdealertin) * 1/100) - (($hdealertin* 1/100) * 5/100);
						echo (empty($har_tin))?'0':number_format($har_tin);
					}
					?></td>
                </tr>
                <?php 
				$omsetifi=0;
				$omsetdealerifi=0;
				foreach ($ifi as $row8):
					     $omsetifi= $omsetifi + $row8->inttotal_omset;
				endforeach;	
				foreach ($ifiDealer as $row9):
					     $omsetdealerifi= $omsetdealerifi + $row9->ifiDealer;
				endforeach;	 
				?>
                <tr>
                	<td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>1 Free 1 Hut/Nett</td>
                    <td align="center">0</td>
                  <td align="center">0</td>
                    <td align="center"><?php echo (empty($omsetifi))?'0':number_format($omsetifi)?></td>
                    <td align="center"><?php echo (empty($omsetdealerifi))?'0':number_format($omsetdealerifi)?></td>
                    <td align="center"><?php echo (empty($omsetdealerifi))?'0':number_format(($omsetdealerifi) * 1/100)?></td>
                    <td align="center"><?php echo (empty($omsetdealerifi))?'0':number_format(($omsetdealerifi * 1/100)*5/100)?></td>
                    <td align="center"><?php 
					$ifiD=0;
					$har_ifi=0;
					if(!empty($omsetdealerifi)){
						$ifiD = $omsetdealerifi;
						$har_ifi = (($omsetdealerifi) * 1/100) - (($omsetdealerifi* 1/100) * 5/100);
						echo (empty($har_ifi))?'0':number_format($har_ifi);
					}
					?></td>
                </tr>
                 <?php 
				$omsetifo10=0;
				$omsetdealerifo=0;
				foreach ($ifo as $row10):
					     $omsetifo10= $omsetifo10 + $row10->TGroup10;
				endforeach;	
				foreach ($ifoDealer as $row11):
					     $omsetdealerifo= $omsetdealerifo + $row11->ifoDealer;
				endforeach;	 
				?>
                 <tr>
                	<td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>1 Free 1 10%</td>
                    <td align="center">0</td>
                    <td align="center"><?php echo (empty($omsetifo10))?'0':number_format($omsetifo10)?></td>
                    <td align="center">0</td>
                    <td align="center"><?php $hdealerifo = ($omsetifo10 - ($omsetifo10*0.1));
					echo (empty($hdealerifo))?'0':number_format($hdealerifo)?></td>
                    <td align="center"><?php echo (empty($hdealerifo))?'0':number_format(($hdealerifo) * 1/100)?></td>
                    <td align="center"><?php echo (empty($hdealerifo))?'0':number_format(($hdealerifo * 1/100)*5/100)?></td>
                    <td align="center"><?php 
					$ifoD=0;
					$har_ifo=0;
					if(!empty($hdealerifo)){
						$ifoD = $hdealerifo;
						$har_ifo = (($hdealerifo) * 1/100) - (($hdealerifo* 1/100) * 5/100);
						echo (empty($har_ifo))?'0':number_format($har_ifo);
					}
					?></td>
                </tr>
                <?php 
				$omsetnet=0;
				$omsetdealernet=0;
				foreach ($net as $row12):
					     $omsetnet= $omsetnet + $row12->inttotal_omset;
				endforeach;	
				foreach ($netDealer as $row13):
					     $omsetdealernet= $omsetdealernet + $row13->netDealer;
				endforeach;	 
				?>
                <tr>
                	<td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>Netto</td>
                    <td align="center">0</td>
                    <td align="center">0</td>
                    <td align="center"><?php echo (empty($omsetnet))?'0':number_format($omsetnet)?></td>
                    <td align="center"><?php echo (empty($omsetdealernet))?'0':number_format($omsetdealernet)?></td>
                    <td align="center"><?php echo (empty($omsetdealernet))?'0':number_format(($omsetdealernet) * 1/100)?></td>
                    <td align="center"><?php echo (empty($omsetdealernet))?'0':number_format(($omsetdealernet * 1/100)*5/100)?></td>
                    <td align="center"><?php 
					$netD=0;
					$har_net=0;
					if(!empty($omsetdealernet)){
						$netD = $omsetdealernet;
						$har_net = (($omsetdealernet) * 1/100) - (($omsetdealernet* 1/100) * 5/100);
						echo (empty($har_net))?'0':number_format($har_net);
					}
					?></td>
                </tr>
                <?php 
				$omsetsp=0;
				$omsetdealersp=0;
				foreach ($sp as $row14):
					     $omsetsp= $omsetsp + $row14->inttotal_omset;
				endforeach;	
				foreach ($spDealer as $row15):
					     $omsetdealersp= $omsetdealersp + $row15->netDealer;
				endforeach;	 
				?>
                <tr>
                	<td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>Special Price</td>
                    <td align="center">0</td>
                    <td align="center">0</td>
                    <td align="center"><?php echo (empty($omsetsp))?'0':number_format($omsetsp)?></td>
                    <td align="center"><?php echo (empty($omsetdealersp))?'0':number_format($omsetdealersp)?></td>
                    <td align="center"><?php echo (empty($omsetdealersp))?'0':number_format(($omsetdealersp) * 1/100)?></td>
                    <td align="center"><?php echo (empty($omsetdealersp))?'0':number_format(($omsetdealersp * 1/100)*5/100)?></td>
                    <td align="center"><?php 
					$spD=0;
					$har_sp=0;
					if(!empty($omsetdealersp)){
						$spD = $omsetdealersp;
						$har_sp = (($omsetdealersp) * 1/100) - (($omsetdealersp* 1/100) * 5/100);
						echo (empty($har_sp))?'0':number_format($har_sp);
					}
					?></td>
                </tr>
                <?php 
				$omsetp=0;
				$omsetdealerp=0;
				foreach ($p as $row16):
					     $omsetp= $omsetp + $row16->inttotal_omset;
						 
				endforeach;	
				foreach ($pDealer as $row31):
					     $omsetdealerp= $omsetdealerp + $row31->netDealer;
				endforeach;	 
				?>
                <tr>
                	<td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>Point</td>
                    <td align="center">0</td>
                    <td align="center">0</td>
                    <td align="center"><?php echo (empty($omsetp))?'0':number_format($omsetp)?></td>
                    <td align="center"><?php echo (empty($omsetdealerp))?'0':number_format($omsetdealerp)?></td>
                    <td align="center"><?php echo (empty($omsetdealerp))?'0':number_format(($omsetdealerp) * 1/100)?></td>
                    <td align="center"><?php echo (empty($omsetdealerp))?'0':number_format(($omsetdealerp * 1/100)*5/100)?></td>
                    <td align="center"><?php 
					$pD=0;
					$har_p=0;
					if(!empty($omsetdealerp)){
						$pD = $omsetdealerp;
						$har_p = (($omsetdealerp) * 1/100) - (($omsetdealerp* 1/100) * 5/100);
						echo (empty($har_p))?'0':number_format($har_p);
					}
					?></td>
                </tr>
				//
			}
			*/
		return $result; 
		}
	}
?>